package com.hoshiicloud.user.service.impl.store;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.platform.Platform;
import com.hoshiicloud.user.entity.store.PlatStore;
import com.hoshiicloud.user.entity.store.StoreDepartment;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.store.StoreDepartmentMapper;
import com.hoshiicloud.user.param.store.StoreDepartmentQueryParam;
import com.hoshiicloud.user.service.IStoreManageService;
import com.hoshiicloud.user.service.impl.StoreManageServiceImpl;
import com.hoshiicloud.user.service.store.PlatStoreService;
import com.hoshiicloud.user.service.store.StoreDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺部门 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-07-24
 */
@Slf4j
@Service
public class StoreDepartmentServiceImpl extends ServiceImpl<StoreDepartmentMapper, StoreDepartment> implements StoreDepartmentService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Autowired
    private PlatStoreService platStoreService;

    @Autowired
    private StoreDepartmentMapper storeDepartmentMapper;

    @Autowired
    private IStoreManageService StoreManageService;

    /**
     * @return IPage
     * @description: 分页查询店铺部门
     */
    @Override
    @Logable(authName = "店铺管理-店铺部门管理-分页查询店铺部门")
    @Cacheable(cacheNames = "StoreDepartment", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public IPage<StoreDepartment> getPage(IPage page, QueryWrapper queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.store.entity.StoreDepartment
     * @description: 根据id查询店铺部门
     */
    @Override
    @Cacheable(cacheNames = "StoreDepartment", key = "#root.targetClass.simpleName + ':' + #root.methodName + ':' + #id")
    @Logable(authName = "店铺管理-店铺部门 -查询店铺部门 ")
    public StoreDepartment getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @return boolean
     * @description: 新增店铺部门
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreDepartment", allEntries = true)
    @Logable(authName = "店铺管理-店铺部门-新增店铺部门")
    public boolean save(StoreDepartment entity) {

        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        PlatStore response = platStoreService.getById(entity.getStoreId());
        if (response==null||"".equals(response)) {
            throw new ServiceException(CommonError.error("新增失败，没有找到店铺信息"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @return boolean
     * @description: 修改店铺部门
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreDepartment", allEntries = true)
    @Logable(authName = "店铺管理-店铺部门管理-修改店铺部门")
    public boolean updateById(StoreDepartment entity) {
        entity.setModifiedTime(LocalDateTime.now());
//        PlatStore response = platStoreService.getById(entity.getStoreId());
//        if (null == response||"".equals(response)) {
//            throw new ServiceException(CommonError.error("修改失败，没有找到平台信息"));
//        }
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }
    @Override
    @Cacheable(cacheNames = "StoreDepartment", key = "#root.targetClass.simpleName + ':' + #root.methodName + ':' + #key")
    @Logable(authName = "店铺管理-店铺部门管理-根据id集合查询店铺部门")
    public List<StoreDepartment> getByIds(List<Long> ids, String key) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 新增店铺部门
     * @param department
     * @return
     */
    @Override
    public Long insert(StoreDepartment department){
        department.setCreatedTime(LocalDateTime.now());
        department.setModifiedTime(LocalDateTime.now());
        department.setCreatedBy("admin");
        department.setModifiedBy("admin");
        int row = baseMapper.insert(department);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增店铺失败"));
        }
        return department.getId();
    }

    /**
     * 根据店铺ID或店铺名称获取店铺所有部门
     * @param param
     * @return
     */
    @Override
    public List<Map> storeDeptList(StoreDepartmentQueryParam param){
        List<Map> storeDeptlist = storeDepartmentMapper.storeDeptList(param);
        return storeDeptlist;
    }

    /**
     * 根据查询条件获取部门名称列表
     * @param param
     * @return
     */
    @Override
    public List<Map> storeDeptNameList(StoreDepartmentQueryParam param){
        List<Map> storeDeptNamelist = storeDepartmentMapper.storeDeptNameList(param);
        return storeDeptNamelist;
    }

}
